package com.sias.config;

import com.sias.interceptor.LogFilter;
import com.sias.interceptor.LogInterceptor;
import com.sias.interceptor.LoginFilter;
import com.sias.interceptor.LoginInterceptor;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.Filter;

/**
 * @author 吴文杰
 * @version 1.0
 * @createTime 2023-05-02 02:27:39
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

  @Bean
  public FilterRegistrationBean<Filter> loginFilterBean(){
    FilterRegistrationBean<Filter> loginFilter = new FilterRegistrationBean<>();
    loginFilter.setFilter(new LoginFilter());
    loginFilter.addUrlPatterns("/*");
    loginFilter.setOrder(2);
    return loginFilter;
  }
  @Bean
  public FilterRegistrationBean<Filter> logFilterBean(){
    FilterRegistrationBean<Filter> logFilter = new FilterRegistrationBean<>();
    logFilter.setFilter(new LogFilter());
    logFilter.addUrlPatterns("/*");
    logFilter.setOrder(1);
    return logFilter;
  }
  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new LogInterceptor())
            .addPathPatterns("/**");
    registry.addInterceptor(new LoginInterceptor())
            .addPathPatterns("/**");
  }
}
